home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / mail / from < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  2.3 KB  |  116 lines

  1. #!/bin/ksh
  2. # @(#) from.ksh 1.1 96/08/09
  3. # from: check for mail; print From: and Subject: lines from message headers.
  4. # 90/12/13 john h. dubois iii (john@armory.com)
  5. # 91/06/09 added check for existance of mailbox
  6. # 91/06/11 fixed check for existance of mailbox, added check for mail
  7. # 91/09/27 added facility for specifying target users or files
  8. # 91/09/28 changed search from egrep expression to sed script for header 
  9. #         recognition; added separation between headers
  10. # 91/11/27 use $MAIL if set
  11. # 92/02/16 added help option
  12. # 96/08/09 Converted to ksh script; added c option.
  13.  
  14. name=${0##*/}
  15. Usage="Usage: $name [-hc] [username ...]"
  16. countOnly=false
  17.  
  18. while getopts hc opt; do
  19.     case $opt in
  20.     h)
  21.     print -r -- \
  22. "$name: tell who mail is from.
  23. For each user named and whose mailbox is readable, $name searches the user's
  24. mailbox and lists the author and subject of each message in the mailbox.
  25. A mailbox can only be searched if the invoking user has read permission for it.
  26. If no username is given, the invoking user's mailbox is checked.
  27. $Usage
  28. Options:
  29. -c: Print a message count only.
  30. -h: Print this help."
  31.     exit 0
  32.     ;;
  33.     c)
  34.     countOnly=true
  35.     ;;
  36.     ?)
  37.     print -r -u2 "Use -h for help."
  38.     exit 1
  39.     ;;
  40.     esac
  41. done
  42.  
  43. # remove args that were options
  44. let OPTIND=OPTIND-1
  45. shift $OPTIND
  46.  
  47. multi=0
  48. case $# in
  49. 0)    set ${MAIL:-$HOME/.mailbox};;
  50. 1)    ;;
  51. *)    multi=1;;
  52. esac
  53.  
  54. while [ $# -gt 0 ]; do
  55.     case $1 in
  56.     [a-z0-9]*) home=`awk -F: 'user == $1 { print $6;}' user=$1 /etc/passwd`;
  57.     mailbox=$home/.mailbox
  58.         if [ -z "$home" ]; then
  59.             echo "No user $1."
  60.         elif [ ! -x $home -o -f $mailbox -a ! -r $mailbox ]; then
  61.         echo "Can't access $1's mailbox."
  62.     else
  63.             targ="$targ $mailbox"
  64.     fi;;
  65.     *)    targ="$targ $1";;
  66.     esac
  67.     shift
  68. done
  69.  
  70. for i in $targ; do
  71.     if [ -f $i ]; then
  72.     if [ -s $i ]; then
  73.         [ $multi -eq 1 ] && echo "$nl$i":
  74.         if $countOnly; then
  75.         awk '
  76. /^/ { newmsg = 1; }
  77. /^From: / { if (newmsg) { count++; newmsg = 0; } }
  78. END { printf "%d message(s) in %s\n",count,FILENAME; }' $i
  79.         else
  80.         sed '
  81. /^/b inhdr
  82. d
  83. :inhdr
  84. /^From /b phdr
  85. /^Subject: /b phdr
  86. b done
  87. : phdr
  88. x
  89. /-----/p
  90. s/-----//
  91. x
  92. p
  93. : done
  94. N
  95. s/.*\n//
  96. /^$/{
  97. s/.*/-----/
  98. h
  99. d
  100. }
  101. b inhdr
  102. ' $i
  103.         fi
  104.     else
  105.         echo "${nl}No mail in $i."
  106.     fi
  107.     else
  108.     if [ $multi -eq 1 ]; then
  109.         echo "$nl$i: no file."
  110.     else
  111.         echo "No mailbox."
  112.     fi
  113.     fi
  114.     nl="\n"
  115. done
  116.